﻿Imports VendasBLL
Public Class ClientesCON
    Public tipo As String
    Public cliente As String
    Public codcli As Integer
    Dim ObjBLL As New ClientesBLL
#Region "Close"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        ObjBLL = Nothing
    End Sub
#End Region
#Region "Carregagrid"
    Sub CarregaGrid()
        ObjBLL = New ClientesBLL
        DGVDados.DataSource = ObjBLL.buscar("", 7)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Load"
    Private Sub ClientesCON_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Limpar()
        CarregaGrid()
        CBFiltro.SelectedIndex = 1
    End Sub
#End Region
#Region "Limpar"
    Sub Limpar()
        TXTLocalizar.Text = ""
        CBFiltro.SelectedIndex = 1
    End Sub
#End Region
#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        ObjBLL = New ClientesBLL
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Nome"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "CPF"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "CNPJ"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Status"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = ObjBLL.buscar("", 7)
        End If
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region
#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        CarregaGrid()
    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
        DGVDados.Refresh()
    End Sub
#End Region

    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try
            If tipo = "ret" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("cli_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        cliente = DGVDados.Rows(indice).Cells("cli_nome").Value.ToString()
                        codcli = DGVDados.Rows(indice).Cells("cli_codigo").Value.ToString()
                        Me.Close()
                    Else
                        Throw New Exception("Esta conta de Centro Custo está Inativo ou foi excluído!.Verifique status em Centro Custo")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ClientesCON_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
End Class